package com.cl.common.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.cl.common.util.StringUtil;
import com.cl.core.orm.Page;
import com.cl.core.web.struts2.Struts2Utils;

public class PageBar extends BodyTagSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6465683101573455492L;
	private String id;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int doEndTag() throws JspException {
		if ((this.bodyContent == null)
				|| (StringUtil.isEmpty(this.bodyContent.getString()))) {
			try {
				this.pageContext.getOut().print(getHtml());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return 6;
	}

	public String getHtml() {
		Page page = (Page) pageContext.getRequest().getAttribute("page");
		int pageNum = page.getPageNo();
		int pageSize = new Long(page.getTotalPages()).intValue();
		StringBuffer content = new StringBuffer();
		content.append("<table width='100%' cellpadding='1' cellspacing='0' border='1' style='border: 1px solid #C6C6C6;border-collapse: collapse;background: url("
				+ Struts2Utils.getRequest().getContextPath()
				+ "/common/image/pagebar/tab_21.gif) #FFFFFF repeat-x left top;table-layout: fixed;'>");
		content.append("<tr >"
				+ "<td colspan='10' align='left'>"
				+ "<input type=\"hidden\" name=\"page.orderBy\" id=\"orderBy\" value=\""
				+ page.getOrderBy()
				+ "\" />"
				+ "<input type='hidden' name='page.order' id='order' value=\""
				+ page.getOrder()
				+ "\" />"
				+ "<input type='hidden' name='page.pageNo' id='pageNo' value=\""
				+ page.getPageNo() + "\" />");
		if (pageNum != 1 && pageNum != 0) {
			content.append("<div style='float: right; font-family: Tahoma'>"
					+ "<img onclick='jumpPage(1)' src='"
					+ Struts2Utils.getRequest().getContextPath()
					+ "/common/image/pagebar/first.gif' style='cursor:hand'/>"
					+ "&nbsp;&nbsp;" + "<s:if test='page.hasPre'>"
					+ "<img onclick='jumpPage(" + page.getPrePage()
					+ ")' src='" + Struts2Utils.getRequest().getContextPath()
					+ "/common/image/pagebar/back.gif' style='cursor:hand'/>"
					+ "&nbsp;&nbsp;" + "</s:if>");
		} else {
			content.append("<div style='float: right; font-family: Tahoma'>"
					+ "<img src='" + Struts2Utils.getRequest().getContextPath()
					+ "/common/image/pagebar/first_dis.gif'/>" + "&nbsp;&nbsp;"
					+ "<s:if test='page.hasPre'>" + "" + "<img src='"
					+ Struts2Utils.getRequest().getContextPath()
					+ "/common/image/pagebar/back_dis.gif'/>&nbsp;&nbsp;"
					+ "</s:if>");
		}

		if (pageNum != pageSize) {

			content.append("" + "<s:if test='page.hasNext'>"
					+ "<img onclick='jumpPage(" + page.getNextPage()
					+ ")' src='" + Struts2Utils.getRequest().getContextPath()
					+ "/common/image/pagebar/next.gif' style='cursor:hand'/>"
					+ "&nbsp;&nbsp;" + "</s:if><img onclick='jumpPage("
					+ page.getTotalPages() + ")' src='"
					+ Struts2Utils.getRequest().getContextPath()
					+ "/common/image/pagebar/last.gif' style='cursor:hand'>"
					+ "&nbsp;&nbsp;&nbsp;&nbsp;转到第&nbsp;");
		} else {
			content.append("" + "<s:if test='page.hasNext'>" + "<img src='"
					+ Struts2Utils.getRequest().getContextPath()
					+ "/common/image/pagebar/next_dis.gif'/>" + "&nbsp;&nbsp;"
					+ "</s:if><img src='"
					+ Struts2Utils.getRequest().getContextPath()
					+ "/common/image/pagebar/last_dis.gif'/>"
					+ "&nbsp;&nbsp;&nbsp;&nbsp;转到第&nbsp;");
		}

		/*
		 * 新增 LHX 2011/5/17 为减少代码量，去掉原SelectTag标签, 所有生成的代码全部在本类中生成
		 * 用select取代原Selector
		 */

		content.append("<select style='width:40px' id='")
				.append(id + "_textField").append("'>");
		for (int i = 1; i <= pageSize; i++) {
			content.append("<option value='").append(i).append("'")
					.append(i == pageNum ? "selected" : "").append(">")
					.append(i).append("</option>");
		}
		content.append("</select>");

		content.append("页");
		if (pageNum == 0) {
			content.append("&nbsp;&nbsp;" + "<img src='"
					+ Struts2Utils.getRequest().getContextPath()
					+ "/common/image/pagebar/go.gif'/>" + "</div>"
					+ "<div style='float: left; font-family: Tahoma'>共 "
					+ page.getTotalCount() + " 条记录，每页 " + page.getPageSize()
					+ " 条，当前第 " + page.getPageNo() + "/" + page.getTotalPages()
					+ " 页</div></td></tr></table>");
		} else {
			content.append("<img src='"
					+ Struts2Utils.getRequest().getContextPath()
					+ "/common/image/pagebar/go.gif'  onclick=\"goToPage('" + id
					+ "');\" style='cursor:hand'/>" + "</div>"
					+ "<div style='float: left; font-family: Tahoma'>共 "
					+ page.getTotalCount() + " 条记录，每页 " + page.getPageSize()
					+ " 条，当前第 " + page.getPageNo() + "/" + page.getTotalPages()
					+ " 页</div></td></tr></table>");
		}
		return content.toString();
	}

}
